public class MySingleList {
    static class ListNodes{
        public int val;
        public ListNodes next;

        public ListNodes() {
        }

        public ListNodes(int val){
            this.val = val;
        }
    }
    public ListNodes head;
    public void creatList(){
        ListNodes node1 = new ListNodes(12);
        ListNodes node2 = new ListNodes(23);
        ListNodes node3 = new ListNodes(34);
        ListNodes node4 = new ListNodes(45);
        ListNodes node5 = new ListNodes(56);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        this.head = node1;
    }
    static class ListNodesA{
        public int val;
        public ListNodes next;
        public ListNodesA(int val){
            this.val = val;
        }
    }



    public void display(){
        ListNodes cur = head;
        while (cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }


    public void display(MySingleList.ListNodes listNodes){
        ListNodes cur = listNodes;
        while (cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }


    public void resever() {
        ListNodes cur = head.next;
        head.next = null;
        //ListNodes curNext = cur.next;
        while (cur != null) {
            ListNodes curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
    }

    public ListNodes midList(){
        ListNodes fast = head;
        ListNodes slow = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    public int size(){
        ListNodes cur = head;
        int count = 0;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }
    public ListNodes returnIndex(int k){
        ListNodes fast = head;
        ListNodes slow = head;
        int count = 0;
        if (k <= 0 || head ==null){
            return null;
        }
        for (int i = 0; i < k-1; i++) {
            fast = fast.next;
            if (fast == null){
                return null;
            }
        }
        while (fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }


    public void addFirst(int data){
        ListNodes nodes = new ListNodes(data);
        nodes.next = head;
        head = nodes;
    }
    public void addLast(int data){
        ListNodes nodes = new ListNodes(data);
        ListNodes cur = head;
        if (cur == null){
            head = nodes;
            return;
        }
        while (cur.next != null){
            cur = cur.next;
        }
        cur.next = nodes;
    }
}
